package com.ds.lens.data.controller;

import com.ds.lens.data.service.LensDatasourceService;
import com.ds.lens.data.vo.BaseOutVO;
import com.ds.lens.data.vo.factory.VOFactory;
import com.ds.lens.data.vo.in.PagedConditionInVO;
import com.ds.lens.data.vo.in.User;
import com.ds.lens.data.vo.in.v2.datasource.LensDatasourceInVO;
import com.ds.lens.data.vo.in.v2.datasource.LensDatasourceListInVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author WeiShaoying
 * @date 2020/5/13 下午2:20
 */
@RestController
@Api(value = "Lens datasource controller")
@RequestMapping(produces = {"application/json;charset=UTF-8"})
public class LensDatasourceController {
    @Autowired
    LensDatasourceService lensDatasourceService;


    @ApiOperation(value = "save datasource")
    @PostMapping(value = "/lens/datasource/save")
    public BaseOutVO saveDatasource(@RequestBody @Valid LensDatasourceInVO inVO,
                                    @RequestAttribute(value = "user") User user) {
        return VOFactory.getSuccessBaseOutVO(lensDatasourceService.saveDatasource(inVO, user));
    }

    @ApiOperation(value = "update datasource")
    @PostMapping(value = "/lens/datasource/update")
    public BaseOutVO updateDatasource(@RequestBody @Valid LensDatasourceInVO inVO,
                                      @RequestAttribute(value = "user") User user) {
        return VOFactory.getSuccessBaseOutVO(lensDatasourceService.updateDatasource(inVO, user));
    }

    @ApiOperation(value = "list datasource")
    @PostMapping(value = "/lens/datasource/list")
    public BaseOutVO listDatasource(@RequestBody @Valid PagedConditionInVO<LensDatasourceListInVO> inVO,
                                    @RequestAttribute(value = "user") User user) {
        return VOFactory.getSuccessBaseOutVO(lensDatasourceService.listDatasource(inVO, user));
    }

    @ApiOperation(value = "get datasource ")
    @GetMapping(value = "/lens/datasource/get")
    public BaseOutVO getDatasource(@RequestParam("id") Long id) {
        return VOFactory.getSuccessBaseOutVO(lensDatasourceService.getDatasource(id));
    }
}
